/**
  ******************************************************************************
  * @file    power.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *           
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"

uint8 TempSecond,WakeSecond;	//20211110 AXD_809
uint8 GetSec(void) //20211110 AXD_809
{
	uint8 temp;
	temp=(uint8_t)(RTC->TR & 0x0000007f);
	temp=(temp/16)*10+(temp%16); //BCD switch to Decimal
	if(temp<1) temp+=60;
	temp-=1;
	return temp;
}

void UserPower_Init(void)
{
	PWR_BGCmd(ENABLE);
	
    PWR_ACMLevelConfig(PWR_ACMLevel_VBG);//PWR_ACMLevel_AVDDR);                //配置ACM输出电压
    PWR_ACMCmd(DISABLE );//DISABLE	                   //使能ACM电路
	
	//PWR_AVDDRLevelConfig(PWR_AVDDRLevel_3V0);
	PWR_AVDDRLevelConfig(PWR_AVDDRLevel_2V4);
	PWR_AVDDRCmd(ENABLE);                      //使能AVDDR输出
	
	PWR_DVDDRLevelConfig(PWR_DVDDRLevel_1V419);//DAVDD 输出电压
    #define BG_CHOP_ENABLE PWR->DLDO_CR |= 0x01<< 21
	#define BG_CHOP_DISABLE PWR->DLDO_CR &= ~(0x01<< 21)
	BG_CHOP_ENABLE;
	UserOPA_Init();
}


void UserVIC_Init(void)
{
	/*VIC_InitTypeDef  VIC_InitStruct;
	VIC_InitStruct.VIC_IRQChannel=EXINT0_IRQn;
	VIC_InitStruct.VIC_IRQChannelPriority=VIC_IRQChannelPriority2;
	VIC_InitStruct.VIC_PrioThreshold=VIC_PrioThreshold0;
	VIC_InitStruct.VIC_PrioThresholdCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelCmd=ENABLE;
	
	VIC_Init(&VIC_InitStruct);
	VIC_IRQChannelPriorityConfig(EXINT1_IRQn,VIC_IRQChannelPriority1);
	
	VIC_IRQChannelCmd(EXINT1_IRQn,ENABLE);
	*/
	
	VIC_IRQChannelCmd(TM0_IRQn,ENABLE);
	VIC_IRQChannelCmd(RTC_IRQn,ENABLE);
	//VIC_IRQChannelCmd(LBT_IRQn,ENABLE);                              //允许LBT发生低压检测中断
	
	VIC_IRQChannelCmd(UART0_IRQn,ENABLE);                            //允许UART发生中断请求
	
}


void UserEXINT_Init(void)
{	
	VIC_InitTypeDef  VIC_InitStruct;
	RCC_APBPeriphClockCmd(RCC_APBPeriph_INT_KEY, ENABLE);      //使能外部中断和按键中断模块的时钟
	
	VIC_InitStruct.VIC_IRQChannel=KEY_IRQn;
	VIC_InitStruct.VIC_IRQChannelPriority=VIC_IRQChannelPriority0;
	VIC_InitStruct.VIC_PrioThreshold=VIC_PrioThreshold0;
	VIC_InitStruct.VIC_PrioThresholdCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;
	VIC_InitStruct.VIC_IRQChannelCmd=ENABLE;
	
	VIC_Init(&VIC_InitStruct);
	
	EXINT_ClearEXITStatus(EXINT_KEY);
	
	/*VIC_InitStructure.VIC_IRQChannel=KEY_IRQn;        //中断向量设置为按键中断
	VIC_InitStructure.VIC_IRQChannelPriority=VIC_IRQChannelPriority0;  //中断优先级设置最高
	VIC_InitStructure.VIC_PrioThreshold=VIC_PrioThreshold0;     //中断阈值最小
	VIC_InitStructure.VIC_PrioThresholdCmd=ENABLE;       //使能中断优先级阈值比较
	VIC_InitStructure.VIC_IRQChannelCmd=ENABLE;        //使能对应编号的中断通道
	VIC_InitStructure.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;    //使能对应编号的中断可唤醒低功耗模式
	VIC_Init(&VIC_InitStructure); 

	EXINT_ClearEXITStatus(EXINT_INT0);                         //清外部中断0(P21)的标志位      
	EXINT_EXITCmd(EXINT_INT0, EXINT_Trigger_Falling,ENABLE);   //使能外部中断0，下降沿中断
	
	EXINT_ClearEXITStatus(EXINT_INT1);                         //清外部中断1(P20)的标志位
	EXINT_EXITCmd(EXINT_INT1, EXINT_Trigger_Rising,ENABLE);    //使能外部中断1，上升沿中断
	*/
}


uint32   halfsec;
void KeyIntDisable(void);
void KeyIntEnable(void);

void tostopmode(void)
{
	
	//DarkAllIcon();
	//while(KEY_POWER&&CHRG&&FULL && KEY_TIME&& KEY_MEMORYUP&&KEY_MEMORYDOWN);//test
#if 1
	USBFlag=0;
	DarkAllIcon();
	BB=POWER | VALVE | PUMP;		
	iocontroloff();  
	AutoTestFlag=0;
	BPTestFlag=0;
	ConnectFlag=0;
	BT_POWER_Off;
	CuffOKFlag=0;		//20221026 add
    SystemSetFlag5.b=0;

	UART_Cmd(UART0,DISABLE);//20220916关闭串口
	
	SoundCommand(0xB6);
	
	if(VoiceEnable) SoundOff();
	
	while(!KEY_POWER||!KEY_TIME||!KEY_MEMORYUP||!KEY_MEMORYDOWN||!KEY_USER);
	keydelay();
	
	if(EndVoice&&(NoEndFlag==0))
	{
		if(VoiceEnable==1&&(VoiceType==1||VoiceType==14))	
		{
			if(VoiceType2==2)
			{
				
			}
			else
			{
				SoundCommand(0xE8);		//谢谢使用。
				SoundCommand(0xF7);
				while(!KEY_POWER||!KEY_TIME||!KEY_MEMORYUP||!KEY_MEMORYDOWN||!KEY_USER);
				keydelay();			
			}
		}
		if(VoiceEnable==1&&VoiceType==9)
		{
			VoiceStop=0;
			SoundCommand(0xF0);
			if(VoiceStop) SoundOff();
			while(!KEY_POWER||!KEY_TIME||!KEY_MEMORYUP||!KEY_MEMORYDOWN||!KEY_USER);
			keydelay();
		}
		if(VoiceEnable==1&&VoiceType==10)
		{
			VoiceStop=0;
			SoundCommand(0x93);	//thank you, wish you a good health
			if(VoiceStop) SoundOff();
			while(!KEY_POWER||!KEY_TIME||!KEY_MEMORYUP||!KEY_MEMORYDOWN||!KEY_USER);
			keydelay();
		}
	}
	NoEndFlag=0;
	TempSecond=GetSec();
Halt:	
	DarkAllIcon();
	GPIO_Stopmode();
	
	//GPIO_ASACMR =0x00;      //关闭模拟口功能
	//GPIO_ASIOUTR=0x00;      //关闭模拟口功能
		//RTC_ClearITStatus(RTC_IT_RTCIE);//清半秒中断标志
		//RTC_ITConfig(RTC_IT_RTCIE,DISABLE);//关闭半秒中断
		
		//RCC_CLKSourceCmd(RCC_CLKSource_XTOSC1,DISABLE);    //关闭XTOSC1  RTC
		RCC_CLKSourceCmd(RCC_CLKSource_XTOSC2,DISABLE);    //关闭XTOSC2
		BUZ_Cmd(BUZ0,DISABLE) ;         //关闭BUZZER0
		BUZ_Cmd(BUZ1,DISABLE) ;         //关闭BUZZER1
		IWDG_Cmd(IWDG_Mode_NoWorking);  //关闭看门狗
		DarkAllIcon();

		//LCD_DeInit();                   //LCD相关寄存器重设为上电默认值(关闭LCD)  影响SEG37/BOOT 造成此段显示
		LCD_Cmd(DISABLE);               //关闭LCD功能，不关闭增加10uA
		PWM_PDM_Cmd(PWM0 ,DISABLE);     //关闭PWM0
		PWM_PDM_Cmd(PWM1 ,DISABLE);     //关闭PWM1

		PWR_BGCmd(DISABLE);             //关闭BG
		PWR_ACMCmd(DISABLE);            //关闭ACM
		PWR_AVDDRCmd(DISABLE);          //关闭AVDDR
		//RTC_Cmd(DISABLE);               //关闭RTC
		
		PWR_LBTCmd(DISABLE);			//关闭比较器
		
		SARADC_DeInit();                //SARADC相关寄存器恢复到上电默认值(关闭SARADC)
		SDADC_DeInit();                 //SDADC相关寄存器恢复到上电默认值(关闭SDADC)

		OPACmd(DISABLE);	//失能OPA输出

		TM_Cmd(TM0, DISABLE);        	//TM2关闭     
		TM_PFDCmd(DISABLE);        		//PFD关闭
		LCD_CPRCmd(DISABLE);            //CPR模块关闭
		TM_ITCmd(TM0,DISABLE);
		/*
		TM_DeInit(TM0);                 //TIMER重设为上电默认值
		TM_DeInit(TM1);                 //TIMER重设为上电默认值
		TM_DeInit(TM2);                 //TIMER重设为上电默认值
		TM_Cmd(TM2, DISABLE);        	//TM2关闭     
		TM_PFDCmd(DISABLE);        		//PFD关闭
		LCD_CPRCmd(DISABLE);            //CPR模块关闭
		TM_ITCmd(TM0,DISABLE);
		VIC_IRQChannelCmd(TM0_IRQn,DISABLE);  //使能VIC外部中断0  
		*/
		WWDG_DeInit();                  //窗口看门狗重设为上电默认值
		
	KeyIntEnable();
	IsMinute=0;
	AlarmFlag=0;
	//halfsec=RTC->TR;//RTC  TEST
	RTC_ClearITStatus(RTC_IT_RTCIE);//清半秒中断标志
	nop4();
	nop4();
	nop4();
//loop:
	DOZE; 
	PWR_Initialization();
	nop4();	
	//halfsec=RTC->TR;//RTC  TEST
	if(IsMinute)
	{
		IsMinute=0;	
		if(TimeInterval<10) TimeInterval=0;
		else
		{
			TimeInterval+=60;
			TimeInterval-=TempSecond;
			TempSecond=0;
		}
		if(AlarmClock) goto WAKE;
	}
	if(!KEY_POWER||!CHRG||!FULL) goto WAKE;
	if(KeyNumber!=6&&KeyNumber!=8)
	{
		if(!KEY_TIME) goto WAKE;
		if(!KEY_USER&&TwoUser) goto WAKE;
		if(KeyNumber!=7)
		{
			if(!KEY_MEMORYUP||!KEY_MEMORYDOWN) goto WAKE;
		}
	}
	goto Halt;
	#if 0
	{ 
		PWR_BGCmd(DISABLE);             //关闭BG
		PWR_ACMCmd(DISABLE);            //关闭ACM
		PWR_AVDDRCmd(DISABLE);          //关闭AVDDR
		PWR_IOUTSCmd(DISABLE);          //关闭恒流输出
		PWR_LBTCmd(DISABLE);			//关闭比较器
		goto loop;
	}
	#endif
WAKE:
	
	RCC_CLKSourceCmd(RCC_CLKSource_XTOSC1,ENABLE);  //RTC
	KeyIntDisable();
	UserPower_Init();
	UserVIC_Init();	
	UserGPIO_Init();
	UserTimer0_Init();
	
	WakeSecond=GetSec();
	if(WakeSecond>=TempSecond) WakeSecond-=TempSecond;
	else WakeSecond=0;
	if(TimeInterval>=10) TimeInterval+=WakeSecond;
	
	//RTC_ClearITStatus(RTC_IT_RTCIE);//清半秒中断标志
	//RTC_ITConfig(RTC_IT_RTCIE,ENABLE);//使能半秒中断
	if(KEY_POWER&&KEY_TIME&&KEY_MEMORYUP&&KEY_MEMORYDOWN&&KEY_USER&&CHRG&&FULL)	//闹钟备用
	{
		for(AlarmN=0;AlarmN<2;AlarmN++)
		{
			if(AlarmHour[AlarmN]==Hour&&AlarmMinute[AlarmN]==Minute&&AlarmOnOff[AlarmN]==1)
			{
				AlarmFlag=1;
				return;
			}
		}
		if(UartType==4)
		{
			BTFlag=0;
			User_Uart0_Init();
			RXIEnable;
			AutoOffTimew=0;
			while(AutoOffTimew<100&&KEY_POWER&&KEY_TIME&&KEY_MEMORYUP&&KEY_MEMORYDOWN&&KEY_USER&&CHRG&&FULL&&!BTFlag);
			if(AutoOffTimew>=100) goto Halt;
		}
		else
		{
			goto Halt;
		}
	}
	
#endif
}


void KeyIntDisable(void)
{
	EXINT_ClearEXITStatus(EXINT_KEY2);
	EXINT_ClearEXITStatus(EXINT_KEY3);
	EXINT_ClearEXITStatus(EXINT_KEY4);
	EXINT_ClearEXITStatus(EXINT_KEY5);
	EXINT_ClearEXITStatus(EXINT_KEY6);
	EXINT_KeyCmd(EXINT_KEY2,DISABLE);//使能按键中断关闭
	EXINT_KeyCmd(EXINT_KEY3,DISABLE);
	EXINT_KeyCmd(EXINT_KEY4,DISABLE);
	EXINT_KeyCmd(EXINT_KEY5,DISABLE);
	EXINT_KeyCmd(EXINT_KEY6,DISABLE);
}


void KeyIntEnable(void)
{
	//EXINT_ClearEXITStatus(EXINT_INT0);
	//EXINT_ClearEXITStatus(EXINT_INT1);
	//EXINT_ClearEXITStatus(EXINT_KEY);
	EXINT_ClearEXITStatus(EXINT_KEY2);
	EXINT_ClearEXITStatus(EXINT_KEY3);
	EXINT_ClearEXITStatus(EXINT_KEY4);
	EXINT_ClearEXITStatus(EXINT_KEY5);
	EXINT_ClearEXITStatus(EXINT_KEY6);
	//EXINT_EXITCmd(EXINT_INT0,EXINT_Trigger_Falling,ENABLE);					//使能外部中断0，下降沿触发,使能，进入低功耗模式时使能
	//EXINT_EXITCmd(EXINT_INT1,EXINT_Trigger_Falling,ENABLE);					//使能外部中断0，下降沿触发,使能，进入低功耗模式时使能
	
	//EXINT_KeyCmd(EXINT_KEY,ENABLE);
	EXINT_KeyCmd(EXINT_KEY2,ENABLE);//使能按键中断
	EXINT_KeyCmd(EXINT_KEY3,ENABLE);
	EXINT_KeyCmd(EXINT_KEY4,ENABLE);
	EXINT_KeyCmd(EXINT_KEY5,ENABLE);
	EXINT_KeyCmd(EXINT_KEY6,ENABLE);
}









#if 0
//POWER配置
void UserPower_Init(void)
{
	RCC_PeriPheralClockCmd(APB_PERIPHCLK_ANACON,ENABLE);  //使能模拟电路时钟	
	
	BG_InitTypeDef     		BG_InitStruct;
	BG_InitStruct.BG_TCK_Bit=DISABLE;    //写0或1实现温度传感器正反测
	BG_InitStruct.BG_TVBS_Bit=DISABLE;   // 设置温度传感器输出buffer的不同尾电流
	BG_InitStruct.BG_TCPEN_Bit=DISABLE;	 // 该位为‘1’时，软件可以通过对TCK写‘1’或‘0’
	BG_InitStruct.BG_TSEL_Bit=DISABLE;	 //选择不同的温度传感器输出信号
	BG_InitStruct.BG_TSOEN_Bit=DISABLE;   //温度传感器输出使能信号,使用软件测试温度传感器时需要对这一位写‘1’才能输出信号。
	
	BG_InitStruct.BG_SCC_Bit=SCC1;      //用于设置BG二阶补偿的幅度。
	BG_InitStruct.BG_CCEN_Bit=DISABLE; //二阶补偿使能信号。
	BG_InitStruct.BG_BGT_Bit=0x0f;      //BG的修调信号。
	BG_InitStruct.BG_BGCLKS_Bit=fana_Div192;  //设置BG的chopper频率,fana/192;
	
	BG_Init(&BG_InitStruct);	
	
	AVDDR_Cmd(ENABLE);             //开启AVDDR
	AVDDR_VoltageOutput(AVDDR_3_0); //设置AVDDR为3.0V
	
	ACM_Cmd(ENABLE);       //开启ACM           
	ACM_VoltageOutput(ACM_AVDDR_Div06); //设置ACM输出电压为0.6*AVDDRA
                  
	DVDDR_VoltageOutput(DVDDR_1_419);   //设置DVDDR输出电压，DVDDR没有使能信号，上电后自动工作
   	
	//UserOPA_Init();
}

#endif

